<template>
  <div>
    <vxe-grid
      ref='xGrid'
      keep-source
      border="border"
      size="mini"
      align="center"
      stripe
      class="mytable-scrollbar"
      id="ExEditTable"
      resizable
      show-overflow
      highlight-hover-row
      :custom-config="tableCustom"
      height="500"
      :export-config="{}"
      :loading="loading"
      :columns="tableColumn"
      :data="tableData"
      :edit-rules="editRules"
      :toolbar-config="tableToolbar"
      :sort-config="{remote:true,trigger:'cell'}"
      :filter-config="{ remote: true }"
      :edit-config="{trigger: 'manual', mode: 'row', showStatus: true, icon: ''}"
      @sort-change="sortChange"
      @edit-closed="editClosed"
    >

     <template #toolbar_buttons>

      <vxe-button icon="fa fa-plus" @click="insertEvent()">新增</vxe-button>

    </template>
    

      <template v-slot:operate="{ row }">
        <template v-if="$refs.xGrid.isActiveByRow(row)">
          <!-- <vxe-button icon="fa fa-save" status="primary" title="保存" circle @click="saveRowEvent(row)"></vxe-button> -->
          <!-- <vxe-button @click="saveRowEvent(row)">保存</vxe-button> -->
          <el-button size="mini" type="primary"  @click="saveRowEvent(row)">保存</el-button>
          
        </template>
        <template v-else>
          <el-button size="mini"  @click="editRowEvent(row)">编辑</el-button>
          <!-- <vxe-button @click="editRowEvent(row)">编辑</vxe-button> -->
          <!-- <vxe-button icon="fa fa-edit" title="编辑" circle @click="editRowEvent(row)"></vxe-button> -->
        </template>
        &nbsp;
        <el-popconfirm
          :confirm-button-text="$t('base.affirm')"
          :cancel-button-text="$t('base.cancel')"
          icon="el-icon-info"
          icon-color="red"
          title="确定删除？"
          @confirm="removeRowEvent(row)"
        >
          <el-button slot="reference" size="mini" type="danger">删除</el-button>
        </el-popconfirm>

        <!-- <vxe-button @click="removeRowEvent(row)">删除</vxe-button> -->
        <!-- <vxe-button icon="fa fa-trash" title="删除" circle @click="removeRowEvent(row)"></vxe-button> -->
        <!-- <vxe-button icon="fa fa-eye" title="查看" circle></vxe-button>
        <vxe-button icon="fa fa-gear" title="设置" circle></vxe-button> -->
      </template>

      

      <!-- 自定义插槽扩展项 -->
      <!-- <slot name="diyslotex" v-bind:row="row">

      </slot> -->

    </vxe-grid>

      

  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'
import XEUtils from 'xe-utils'

export default {
  name: 'ExEditTable',
  components: {
    //PageHeaderLayout,
  },
  props: {
    /* 加载数据状态 */
    loading: {
      type: Boolean,
      default: false
    },
    /* 表格列定义 */
    tableColumn: {
      type: Array,
      default() {
        return []
      }
    },
    /* 表格数据 */
    tableData: {
      type: Array,
      default() {
        return []
      }
    },
    editRules:{
      type: Object,
      default() {
        return {}
      }
    }
    /* 表底部函数 */
    // footerMethod: {
    //   type: Function
    // }
  },
  // 定义属性
  data() {
    return {

      tableToolbar: {
        custom: true,
        slots: {
          buttons: 'toolbar_buttons'
        }
      },
      tableCustom: {
        storage: true
      },
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    sortChange({column, property, order, sortBy, sortList, $event}) {
      // property：字段名 order：排除规则
      console.log("排序排序排序排序排序排序", property);
      this.$emit('customSortMethodEmit', property, order)
    },
    // 编辑
    editRowEvent (row) {
      this.$emit('editRowEventEmit', row)
      // this.$refs.xGrid.setActiveRow(row)
    },
    editClosed(row){
      console.log('编辑状态关闭后',row);
    },
    // 保存
    async saveRowEvent (row) {

      const errMap = await this.$refs.xGrid.validate(row).catch(errMap => errMap)
      if (errMap) {
         this.$message({
          showClose: true,
          message: '校验不通过！',
          type: 'warning'
        });
        // this.$XModal.message({ status: 'error', message: '校验不通过！' })
      } else {
        await this.$refs.xGrid.clearActived().then(() => {
          this.$emit('saveRowEventEmit', row)
        })

        // this.$XModal.message({ status: 'success', message: '校验成功！' })
      }
      
    },
    async insertEvent (row) {


      // let record = {
      //   // sex: '1'
      //   in_vrb_name:'23'
      // }
      // let { row: newRow } = await this.$refs.xGrid.insertAt(record, row)
      // console.log('newrow',newRow);
      // await this.$refs.xGrid.setActiveRow(newRow)

      // console.log('newrow',newRow);



      this.$emit('insertEventEmit', row)
      // const $table = this.$refs.xGrid
      // const record = {
      //   // sex: '1',
      //   // date12: '2021-01-01'
      // }
      // const { row: newRow } = await $table.insertAt(record, null)
      // await $table.setActiveCell(newRow)
    },

    // 移除
    removeRowEvent (row) {
      if (XEUtils.isEmpty(row.id)) {
        this.$refs.xGrid.remove(row)
      }else{
        this.$emit('removeRowEventEmit', row)
      }
      // console.log('row',row);
      
      // this.$XModal.confirm('您确定要删除该数据?').then(type => {
      //   if (type === 'confirm') {
      //     
      //   }
      // })
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
/*滚动条整体部分*/
.mytable-scrollbar ::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

/*滚动条的轨道*/
.mytable-scrollbar ::-webkit-scrollbar-track {
  background-color: #FFFFFF;
}

/*滚动条里面的小方块，能向上向下移动*/
.mytable-scrollbar ::-webkit-scrollbar-thumb {
  background-color: #bfbfbf;
  border-radius: 5px;
  border: 1px solid #F1F1F1;
  box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
}

.mytable-scrollbar ::-webkit-scrollbar-thumb:hover {
  background-color: #A8A8A8;
}

.mytable-scrollbar ::-webkit-scrollbar-thumb:active {
  background-color: #787878;
}

/*边角，即两个滚动条的交汇处*/
.mytable-scrollbar ::-webkit-scrollbar-corner {
  background-color: #FFFFFF;
}
</style>